﻿/// <reference path="Turbo_common.js" />
/// <reference path="PostScenario.js" />

TA.Turbo.Common._DoPostBack_Page = function _DoPostBack_Page(eventTarget, eventName, eventArgument, scenario)
{
	if (_____a)
		scenario = new $PostScenario();

	// ensure special properties
	for (var iPostControl = 0; iPostControl < scenario.PostControls.length; ++iPostControl)
	{
		var pc = scenario.PostControls[iPostControl];
		if (_____a)
			pc = new $PostControl();
		if (pc.Value)
		{
			var controlSerValue = pc.get_Control().get_Object().get_SerializedValue();
			var postingElement = pc.get_Control();

			if (pc.get_Control().tagName == "INPUT" || pc.get_Control().tagName == "SELECT" || pc.get_Control().tagName == "TEXTAREA")
				pc.get_Control().value = controlSerValue;
			else
			{
				postingElement = TA.Turbo.Common.Init_CreateHidden(pc.get_Control().name, controlSerValue);
			}
		}
		if (pc.SendAs != null)
			postingElement.name = pc.SendAs;

		var stateElement = document.getElementById(pc.get_Control().id + TA.Turbo.Common.StateFieldClientIdPostfix);
		if (pc.Properties.length > 0)
		{
			if (stateElement == null)
			{
				stateElement = TA.Turbo.Common.Init_CreateHidden(TA.Turbo.Common.GetControlServerName(pc.get_Control()) + TA.Turbo.Common.StateFieldUniqueIdPostfix, null);
			}

			stateElement.value = TA.Turbo.Common._GetPropertiesJson(pc);
		}
		else
		{
			if (stateElement != null)
				stateElement.parentNode.removeChild(stateElement);
		}
	}

	// remove unwanted elements from post
	for (var iFormElement = 0; iFormElement < theForm.elements.length; ++iFormElement)
	{
		var elem = theForm.elements[iFormElement];
		if (elem.name == "__T"
			||
			elem.name == "__N"
			||
			elem.name == "__A"
			||
			elem.name == "__EVENTVALIDATION"
			||
			elem.name == "__VIEWSTATE"
			||
			elem.name == eventTarget)
			continue;

		var found = false;
		for (var iPostControl = 0; iPostControl < scenario.PostControls.length; ++iPostControl)
		{
			var pc = scenario.PostControls[iPostControl];
			if (_____a)
				pc = new $PostControl();

			if (pc.get_Control().name == elem.name && pc.Value) { found = true; break; }

			var foundProperty = false;
			for (var iProperty = 0; iProperty < pc.Properties.length; ++iProperty)
			{
				if (pc.get_Control().name + "$$$" + pc.Properties[iProperty] == elem.name) { foundProperty = true; break; }
			}
			if (foundProperty) { found = true; break; }
		}
		if (!found)
		{
			elem.name = null;
			elem.value = null;
			//			elem.parentNode.removeChild(elem);
			//			--iFormElement;
		}
	}

	if (scenario.Clean)
	{
		//		theForm.__VIEWSTATE.parentNode.removeChild(theForm.__VIEWSTATE);
		theForm["__T"].parentNode.removeChild(theForm["__T"]);
		theForm["__N"].parentNode.removeChild(theForm["__N"]);
		theForm["__A"].parentNode.removeChild(theForm.__A);
		//		if (typeof (theForm.__EVENTVALIDATION) != "undefined")
		//			theForm.__EVENTVALIDATION.parentNode.removeChild(theForm.__EVENTVALIDATION);
	}
	else
	{
		TA.Turbo.Common.Init_CreateHidden("__TURBO", 1);
		//		TA.Turbo.Common.Init_CreateHidden("__SCENARIO", scenario.Id);
		theForm["__T"].value = eventTarget;
		theForm["__N"].value = eventName;
		theForm["__A"].value = eventArgument;
	}
	theForm.action = scenario.PostUrl;
	if (scenario.SendHashString)
	{
		var mHashString = TA.Turbo.Common.RgxGetHashString.exec(location.href);
		if (mHashString != null)
			theForm.action = Ajax.AppendToUrl(theForm.action, mHashString[1]);
	}

	theForm.method = scenario.HttpVerb == HttpVerb.Post ? "POST" : "GET";

	theForm.submit();
}

